<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('用户修改')"/>
</head>
<body>
<form action="" class="layui-form" th:object="${TsysUser}">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">编号</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="title" name="id"
                               placeholder="请输入标题" th:value="*{id}" type="text">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">登录名称</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="username" name="username"
                               placeholder="登录名称" th:value="*{username}" type="text">
                    </div>

                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">昵称</label>
                    <div class="layui-input-block">
                        <input autocomplete="off" class="layui-input" lay-verify="nickname" name="nickname"
                               placeholder="请输入昵称" th:value="*{nickname}" type="text">
                    </div>
                </div>

                <div class="layui-form-item" id="userTypeItem">
                    <label class="layui-form-label">账户属性</label>
                    <div class="layui-input-block">
                        <select id="userType" name="userType" lay-filter="userTypeSelect" th:field="*{userType}">
                            <option value=""></option>
                            <option value="0">管理员</option>
                            <option value="1">供电所</option>
                            <option value="2">审核部门</option>
                            <option value="3">分公司</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item" id="depIdItem">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-input-block">
                        <!--                        <ul id="selectParent" name="deptId" class="dtree" data-id="0"></ul>-->
                        <input lay-skin="primary" name="depId" th:checked="${dep.ischeck}" th:each="dep:${depList}"
                               th:title="${dep.deptName}" th:value="${dep.id}" type="checkbox">
                    </div>
                </div>

                <div class="layui-form-item" id="supplyIdItem">
                    <label class="layui-form-label">供电所</label>
                    <div class="layui-input-block">
                        <!--                        <ul id="selectParent" name="deptId" class="dtree" data-id="0"></ul>-->
                        <input lay-skin="primary" name="supplyId" th:checked="${supply.ischeck}" th:each="supply:${supplyList}"
                               th:title="${supply.name}" th:value="${supply.id}" type="checkbox">
                    </div>
                </div>

                <div class="layui-form-item" id="subCompanyItem">
                    <label class="layui-form-label">分公司</label>
                    <div class="layui-input-block">
                        <!--                        <ul id="selectParent" name="deptId" class="dtree" data-id="0"></ul>-->
                        <input lay-skin="primary" name="subCompanyId" th:checked="${subCompany.ischeck}" th:each="subCompany:${subCompanyList}"
                               th:title="${subCompany.name}" th:value="${subCompany.id}" type="checkbox">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block">
                        <input lay-skin="primary" name="roleId" th:checked="${sysRole.ischeck}"
                               th:each="sysRole:${roleVos}" th:title="${sysRole.name}"
                               th:value="${sysRole.id}" type="checkbox">
                    </div>
                </div>

            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button class="layui-btn layui-btn-normal layui-btn-sm" lay-filter="user-update" lay-submit=""
                    type="submit">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button class="layui-btn layui-btn-primary layui-btn-sm" type="reset">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<th:block th:include="include :: footer"/>
<script>

    var userType = '[[${TsysUser.userType}]]';

    if (userType == '0') {
        $("#depIdItem").hide();
        $("#supplyIdItem").hide();
        $("#subCompanyItem").hide();
    } else if (userType == '1') {
        $("#depIdItem").hide();
        $("#supplyIdItem").show();
        $("#subCompanyItem").hide();
    } else if (userType == '2') {
        $("#depIdItem").show();
        $("#supplyIdItem").hide();
        $("#subCompanyItem").hide();
    } else if (userType == '3') {
        $("#depIdItem").hide();
        $("#supplyIdItem").hide();
        $("#subCompanyItem").show();
    }


    layui.use(['form', 'jquery', 'dtree'], function () {
        var form = layui.form;
        var $ = layui.jquery;
        var dtree = layui.dtree;
        var MODULE_PATH = rootPath + "/UserController";
        var DEPAR_PATH = rootPath + "/SysDepartmentController";
        var username = '[[${TsysUser.username}]]';
        //部门树
        // dtree.renderSelect({
        //     elem: "#selectParent",
        //     url: DEPAR_PATH+"/tree",
        //     method: 'get',
        //     showCheckbox: true,
        //     selectInputName: {nodeId: "depId"},
        //     skin: "layui",
        //     dataFormat: "list",
        //     response: {treeId: "id", parentId: "parentId", title: "deptName"},
        //     selectInitVal: "[[${TsysUser.depId}]]"
        // });


        //名称验证
        form.verify({
            username: function (value) {
                if (value.length < 2) {
                    return '标题至少得2个字符啊';
                }
                if (username != value) {
                    var msg = "";
                    $.ajax({
                        url: MODULE_PATH + '/checkLoginNameUnique',
                        data: "username=" + value,
                        dataType: 'json',
                        type: 'post',
                        async: false,
                        success: function (result) {
                            msg = result;
                        }
                    });
                    if (msg == 1) {
                        return "已有相同数据";
                    }
                }


            }
        });

        form.on('select(userTypeSelect)', function (data) {
            if (data.value == '0') {
                $("#depIdItem").hide();
                $("#supplyIdItem").hide();
                $("#subCompanyItem").hide();
            } else if (data.value == '1') {
                $("#depIdItem").hide();
                $("#supplyIdItem").show();
                $("#subCompanyItem").hide();
            } else if (data.value == '2') {
                $("#depIdItem").show();
                $("#supplyIdItem").hide();
                $("#subCompanyItem").hide();
            } else if (data.value == '3') {
                $("#depIdItem").hide();
                $("#supplyIdItem").hide();
                $("#subCompanyItem").show();
            }
        });

        form.on('submit(user-update)', function (data) {
            //角色数据拼接
            var roleIds = "";
            $('input:checkbox[name="roleId"]:checked').each(function () {
                roleIds += $(this).val() + ",";
            });
            roleIds = roleIds.substr(0, roleIds.length - 1);
            data.field.roleIds = roleIds;

            //账户类型
            var type = data.field.userType;


            var depIds = "";
            var supplyIds = "";
            var subCompanyIds = "";

            if (type=="1"){
                //供电所数据拼接
                $('input:checkbox[name="supplyId"]:checked').each(function () {
                    supplyIds += $(this).val() + ",";
                });
                supplyIds = supplyIds.substr(0, supplyIds.length - 1);
                // data.field.supplyIds = supplyIds;
                var supply = supplyIds.split(',')
                if (supply.length!=1){
                    layer.msg('绑定的供电所只能选择1个', {icon: 2, time: 1000});
                    return false;
                }
            } else if (type=="2"){
                //部门数据拼接
                $('input:checkbox[name="depId"]:checked').each(function () {
                    depIds += $(this).val() + ",";
                });
                depIds = depIds.substr(0, depIds.length - 1);
                // data.field.depIds = depIds;

                var dept = depIds.split(',')
                if (dept.length!=1){
                    layer.msg('绑定的部门只能选择1个', {icon: 2, time: 1000});
                    return false;
                }
            } else if (type=="3"){
                //分公司数据拼接
                $('input:checkbox[name="subCompanyId"]:checked').each(function () {
                    subCompanyIds += $(this).val() + ",";
                });
                subCompanyIds = subCompanyIds.substr(0, subCompanyIds.length - 1);
                data.field.subCompanyIds = subCompanyIds;

                var subCompany = subCompanyIds.split(',')
                if (subCompany.length!=1){
                    layer.msg('绑定的分公司只能选择1个', {icon: 2, time: 1000});
                    return false;
                }
            }

            data.field.supplyIds = supplyIds;
            data.field.depIds = depIds;
            data.field.subCompanyIds = subCompanyIds;

            $.ajax({
                url: MODULE_PATH + '/edit',
                data: data.field,
                dataType: 'json',
                contentType: 'application/x-www-form-urlencoded',
                type: 'post',
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("user-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    })
</script>
<script>
</script>
</body>
</html>